nums = [1, 2, 3, 4]


// var productExceptSelf = function (nums) {
//     let len = nums.length
//     let answer = []
//     let L = [], R = []
//     L[0] = 1, R[len - 1] = 1
//     for (let i = 1; i < len; i++) {
//         L[i] = nums[i - 1] * L[i - 1]
//     }
//     for (let j = len - 2; j >= 0; j--) {
//         R[j] = nums[j + 1] * R[j + 1]
//     }
//     for (let m = 0; m < len; m++) {
//         answer[m] = L[m] * R[m]
//     }
//     return answer
// };

// var productExceptSelf = function (nums) {
//     let len = nums.length
//     let answer = Array(len)
//     answer[0] = 1
//     for (let i = 1; i < len; i++) {
//         answer[i] = answer[i - 1] * nums[i - 1]
//     }
//     let right = 1
//     for (let i = len - 1; i >= 0; i--) {
//         answer[i] = answer[i] * right
//         right *= nums[i]
//     }
//     return answer
// }

var productExceptSelf = function (nums) {
    let len = nums.length;
    let answer = new Array(len);
    answer[0] = 1;

    for (let i = 1; i < len; i++) {
        answer[i] = answer[i - 1] * nums[i - 1];
    }

    let rightCount = 1;
    for (let i = len - 1; i >= 0; i--) {
        answer[i] = answer[i] * rightCount;
        rightCount *= nums[i]
    }
    return answer;
}
console.log(productExceptSelf(nums));
